<template>
  <div>
    <h1>学生管理</h1>
    <div style="margin-top: 15px">
      <el-input

        v-model="values"
         placeholder="请输入内容"
        class="input-with-select"
      >
        <el-select v-model="type" slot="prepend" placeholder="请选择">
          <el-option label="姓名" value="name"></el-option>
          <el-option label="年龄" value="age"></el-option>
          <el-option label="性别" value="gender"></el-option>
        </el-select>
        <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
      </el-input>
    </div>

    <el-table :data="studentDate" border style="width: 100%">
      <el-table-column prop="_id" label="学号" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="gender" label="性别" width="180">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
      <el-table-column prop="subjectsId.name" label="专业" width="180">
        <template slot-scope="scope">
          {{ scope.row.subjectsId ? scope.row.subjectsId.name : "暂无" }}
        </template>
      </el-table-column>
      <el-table-column prop="classesId.name" label="班级" width="180">
        <template slot-scope="scope">
          {{ scope.row.classesId ? scope.row.classesId.name : "暂无" }}
        </template>
      </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <el-avatar size="large" :src="scope.row.imagePath"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            v-auth
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageDate.currentPage"
      :page-sizes="[1, 5, 8, 10]"
      :page-size="pageDate.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";

import pageMixins from "@/mixins/pageMixins.js";
const { mapActions: studentActions, mapState: studentState } =
  createNamespacedHelpers("student");
export default {
  mixins: [pageMixins],
  created() {
    this.getStudentList();
  },
  methods: {
    ...studentActions(["getStudentsAsync", "delStudentsAsync"]),

    getStudentList() {
      this.getStudentsAsync(this.pageDate);
    },

    //修改
    handleEdit(index, row) {
      this.$router.push("/home/studentUpdate/" + row._id);
    },
    //删除
    async handleDelete(index, row) {
      console.log(index, row);
      const data = await this.delStudentsAsync({ _id: row._id });
      if (data.code) {
        alert(data.message);
      }
      this.getStudentList();
    },
    //搜索
    search(){
        console.log({
            ...this.pageDate,
            [this.type]:this.values
        });
          this.getStudentsAsync(
            {
              ...this.pageDate,
               type:this.type,
               value:this.values
            
            });
    }
  },
  computed: {
    ...studentState({
      studentDate: "rows",
      pages: "pages",
      total: "total",
    }),
  },
  
   data() {
    return {
        type:'name',
        values:'',

    }
  },

  watch: {
    pageDate: {
      handler() {
        this.getStudentList();
      },
      deep: true,
    },
  },
  
};
</script>

<style scoped>
 .el-input {
    width: 500px;
    margin-bottom: 20px;
  }
   .el-select{
     width: 100px;
   }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>